package com.briup.jz.service.impl;


import com.briup.jz.bean.RolePrivilege;
import com.briup.jz.bean.RolePrivilegeExample;
import com.briup.jz.bean.extend.RolePrivilegeExtend;
import com.briup.jz.dao.RolePrivilegeMapper;
import com.briup.jz.dao.extend.RolePrivilegeExtendMapper;
import com.briup.jz.service.IRolePrivilegeService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RolePrivilegeImpl implements IRolePrivilegeService {
    @Resource
    private RolePrivilegeMapper rolePrivilegeMapper;
    @Resource
    private RolePrivilegeExtendMapper rolePrivilegeExtendMapper;

    @Override
    public List<RolePrivilege> findAll() {
        return rolePrivilegeMapper.selectByExample(new RolePrivilegeExample());
    }

    @Override
    public RolePrivilege findById(long id) {
        return rolePrivilegeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrEdit(RolePrivilege rolePrivilege) throws CustomerException {
        if (rolePrivilege.getId()==null){
            rolePrivilegeMapper.insert(rolePrivilege);
        } else {
            if (rolePrivilegeMapper.selectByPrimaryKey(rolePrivilege.getId())==null){
                throw new CustomerException("修改失败，数据不存在");
            }
            rolePrivilegeMapper.updateByPrimaryKey(rolePrivilege);
        }
    }

    @Override
    public List<RolePrivilegeExtend> findAllWithRoleAndPrivilege() {
        return rolePrivilegeExtendMapper.selectAllWithRoleAndPrivilege();
    }

    @Override
    public void delById(long id) throws CustomerException {
        if (rolePrivilegeMapper.selectByPrimaryKey(id)==null){
            throw new CustomerException("删除失败，数据不存在");
        }
        rolePrivilegeMapper.deleteByPrimaryKey(id);
    }
}